/*******************************************************************************
 * 文件名: ExcelExport.java
 * 描    述: 导出Excel
 * 创建人: Dean Huang(hudi-112@163.com)
 * 日　期: 2018-01-11
 * 描　述: 文件描述
 * 版　本: 1.0
 *******************************************************************************/

package com.z1.core.data.export.excel;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.z1.core.data.DataConfigCell;
import com.z1.core.reflect.BeanReflect;

public class ExcelExport {

	public final static int EXCEL2003 = 2003;
	public final static int EXCEL2007 = 2007;
	//excel文本对象
	private Workbook workbook = null;
	//表头样式
	private CellStyle headStyle = null;
	//表头
	private String[] head = null;
	//当前写入表对象
	private Sheet sheet = null;
	//当前写入表数
	private int sheetInd = 0;
	//当前写入行数
	private int rowInd = 0;
	
	public ExcelExport(String[] head) {
		this(EXCEL2003, head);
	}
	public ExcelExport(String[] head, String password) {
		this(EXCEL2003, head, password);
	}
	public ExcelExport(int version, String[] head) {
		this(EXCEL2003, head, null);
	}
	public ExcelExport(int version, String[] head, String password) {
		if (EXCEL2003 == version) {
			if (null == password) {
				workbook = new HSSFWorkbook();
			}
			else {
				HSSFWorkbook wb = new HSSFWorkbook();
				wb.writeProtectWorkbook(password, "");
				workbook = wb;
			}
		}
		else if (EXCEL2007 == version) {
			if (null == password) {
				workbook = new XSSFWorkbook();
			}
			else {
				XSSFWorkbook wb = new XSSFWorkbook();
				wb.setWorkbookPassword(password, null);
				workbook = wb;
			}
		}
		//head cellstyle
		headStyle = workbook.createCellStyle();
		headStyle.setFillForegroundColor((short)17);
		headStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		Font headFont = workbook.createFont();
		headFont.setColor(Font.COLOR_NORMAL);
		headFont.setFontHeightInPoints((short)10);
		headFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		headStyle.setFont(headFont);
		
		setHeads(null, head);
	}
	
	public void setHeads(String title, String[] heads) {
		this.head = heads;
		newSheet(title);
	}
	
	/**
	 * 输出图片对象
	 * @param bi
	 */
	public void image(BufferedImage bi) {
		if (null != bi) {
			try {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ImageIO.write(bi, "png", baos);
				sheet = workbook.createSheet("图表");
				Drawing drawing = sheet.createDrawingPatriarch();
				ClientAnchor anchor = drawing.createAnchor(0, 0, 512, 255, (short)1, 1, (short)10, 20);
				anchor.setAnchorType(2);
				drawing.createPicture(anchor , workbook.addPicture(baos.toByteArray(), Workbook.PICTURE_TYPE_JPEG));
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 导出数据
	 * @param title 数据标题
	 * @param values 数据集
	 */
	public void export(String title, String[] values) {
		if (null == sheet || rowInd >= 6000) {
			//创建新表
			newSheet(title);
		}
		//创建新行写数据
		Row row = sheet.createRow(rowInd++);
		for (int i = 0; i < values.length; i++) {
			Cell cell = row.createCell(i);
			cell.setCellType(Cell.CELL_TYPE_STRING); 
			cell.setCellValue(values[i]);
		}
	}
	public void export(String[] values) {
		export(null, values);
	}
	public void export(String title, Object obj, List<DataConfigCell> cells) {
		String[] values = new String[cells.size()];
		for (int i = 0; i < cells.size(); i++) {
			DataConfigCell cell = cells.get(i);
			Object valueObj = BeanReflect.getObjectFromT(obj, cell.getProperty());
			values[i] = null == valueObj ? "" : valueObj.toString();
		}
		export(null, values);
	}
	public void export(Object obj, List<DataConfigCell> cells) {
		export(null, obj, cells);
	}
	
	/**
	 * 创建新表
	 * @param title 数据标题
	 */
	public void newSheet(String title) {
		if (null == title) {
			title = "data_" + sheetInd++;
		}
		if (null == workbook.getSheet(title)) {
			sheet = workbook.createSheet(title);
			rowInd = 0;
			
			//创建新行写表头
			Row row = sheet.createRow(rowInd++);
			for (int i = 0; i < head.length; i++) {
				Cell cell = row.createCell(i);
				cell.setCellType(Cell.CELL_TYPE_STRING); 
				cell.setCellValue(head[i].toString());
				cell.setCellStyle(headStyle);
			}
		}
	}
	
	/**
	 * 输出至流
	 * @param title 数据标题
	 * @param output 流对象
	 * @throws Exception
	 */
//	public void export(String title, OutputStream output) throws Exception {
//		if (0 == sheetInd && 0 == rowInd) {
//			newSheet(null);
//		}
//		workbook.write(output);
//	}
	public void export(OutputStream output) throws Exception {
//		export(null, output);
		workbook.write(output);
	}
	
}
